﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Public;
using DLL;

namespace QLBanVe
{
    public partial class frmThemTuyenBay : Form
    {
        public frmThemTuyenBay()
        {
            InitializeComponent();
        }
        TuyenBayPublic p = new TuyenBayPublic();
        TuyenBayDLL tb = new TuyenBayDLL();
        SanBayDLL sb = new SanBayDLL();
        
        string SanBayDi = "";
        string SanBayDen = "";
        string Ma = "";
        string MaTuyenBay = "";
        
        private void frmThemTuyenBay_Load(object sender, EventArgs e)
        {
            
            txtMaTuyenBay.Enabled = false;
            txtMaTuyenBay.Text = "";
            ///Load combobox
           
            cbSanBayDi.DataSource = sb.LoadSanBay();
            cbSanBayDi.DisplayMember = "TenSanBay";
            cbSanBayDi.ValueMember = "MaSanBay";
            cbSanBayDi.SelectedIndex = -1;
            //
            cbSanBayDen.DataSource = sb.LoadSanBay();
            cbSanBayDen.DisplayMember = "TenSanBay";
            cbSanBayDen.ValueMember = "MaSanBay";
            cbSanBayDen.SelectedIndex = -1;
        }

        private void cbSanBayDi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cbSanBayDi.SelectedIndex!=-1)
            ChonCB();
        }
        private void ChonCB()
        {
            DataTable dt = new DataTable();
            if (cbSanBayDi.SelectedIndex != -1)
            {
                int i = cbSanBayDi.SelectedIndex;
                dt = sb.LoadSanBay();
                DataRow dr;
                dr = dt.Rows[i];
                MaTuyenBay = dr["MaSanBay"].ToString()+"-";
                SanBayDi = dr["MaSanBay"].ToString();
               
            }

        }
        private void ChonCB1()
        {
            DataTable dt = new DataTable();
            if (cbSanBayDen.SelectedIndex != -1)
            {
                int i = cbSanBayDen.SelectedIndex;
                dt = sb.LoadSanBay();
                DataRow dr;
                dr = dt.Rows[i];
                txtMaTuyenBay.Text = MaTuyenBay + dr["MaSanBay"].ToString();
                Ma = txtMaTuyenBay.Text;
                SanBayDen = dr["TenSanBay"].ToString();
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbSanBayDen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbSanBayDen.SelectedIndex != -1)
                ChonCB1();
            else
                txtMaTuyenBay.Text = "";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {

            try
            {
                if (cbSanBayDi.SelectedValue.ToString() == cbSanBayDen.SelectedValue.ToString())
                {
                    MessageBox.Show("Sân bay đi và sân bay đến không được trùng");
                }
                else
                {
                p.MaTuyenBay = Ma;
                p.SanBayDi = SanBayDi;
                p.SanBayDen = SanBayDen;
                int i = tb.ThemTuyenBay(p);
                if (i > 0)
                {
                    cbSanBayDi.Text = "";
                    cbSanBayDen.Text = "";
                    txtMaTuyenBay.Text = "";
                }
                }
            }
            catch
            {
                MessageBox.Show("Tuyến bay đã tồn tại");
            }
        }
    }
}
